<template>
    <rx-boxs v-if="!readOnly" v-model="curValue" @select="handDialog" @buttonSelect="buttonSelect" :read-only="readOnly"
             :single="single" :singleValue="isSingle" :icon="icon" :type="'btnEdit'" buttonedit="true"></rx-boxs>
    <view v-else class="rx-disabled">
        {{curValue }}
    </view>
</template>

<script>
import Ajax from '@/assets/ajax.js';
import CustomQuery from "@/views/modules/form/customForm/js/CustomQuery.js";
import MForm from "../js/MForm";
import Dialog from "@/js/Dialog.js";
import baseButtonFunc from "@/js/baseButtonFunc.js"
import LinkUrl from "@/js/LinkUrl"
import RxBoxs from "../../form/common/RxBoxs.vue";
export default {
    name: "MButtonSelectSingle",
    mixins: [MForm,baseButtonFunc,LinkUrl],
    components: {
        RxBoxs,
    },
    data() {
        return {
            curValue:"",
            isSingle:true,
            itemConfig:{},
            single: true,
            icon:'',

        }
    },
    created() {
        this.itemConfig = this.metadata;
        this.icon = this.metadata.options.checkedChildren?this.metadata.options.checkedChildren:'';
    },
    methods:{
        buttonSelect(val){
            this.curValue = val;
            this.setData(this.curValue);
        },
        handDialog(){
            if(!this.itemConfig.options.dialogConfig){
                return;
            }
            let dialogConfig = JSON.parse(this.itemConfig.options.dialogConfig);
            if(dialogConfig.beforeMethod && this.isBeforeMethod(dialogConfig.beforeMethod)){
                return;
            }
            if(!dialogConfig.dialogAlias){
                return;
            }
            let dialogAlias = JSON.parse(dialogConfig.dialogAlias).value;
            let gridInput = dialogConfig.gridInput;
            /*如果输入条件有必填，则提示*/
            if(this.isConditionRequired(gridInput,this.formVm.data,this.data)){
                return;
            }
            //构建查询参数
            let params = CustomQuery.getParamsInput(gridInput,this.formVm.formData?this.formVm.formData:this.formVm.data,this.data,this.formVm);
            let self_=this;
            let parameter = {
                alias: dialogAlias,
                single:dialogConfig.singleType == 'single'?true:false,
                params: Object.keys(params).map(function (key) {
                    return key + "=" + params[key];
                }).join("&"),
                onload: function () {
                    this.$nextTick(function () {
                        let isDialog = this.formBoList.isDialog;
                        let isTreeDlg = this.formBoList.isTreeDlg;
                        let idField = this.formBoList.idField;
                        let idValue="";
                        if(self_.curValue){
                            idValue=JSON.parse(self_.curValue).value;
                        }
                        if(!idValue){
                            return;
                        }
                        let params={};
                        let queryParams={};
                        queryParams["Q_"+dialogConfig.valueField+"_S_IN"]=idValue;
                        if(this.formBoList.isTreeDlg=='YES'){
                            params=queryParams;
                        }else{
                            params={pageNo: 1, pageSize: 1000, params:queryParams};
                        }
                        Ajax.postJson(this.table.url,params).then(res=>{
                            let curDataSource=res.result.data;
                            if (isTreeDlg == 'YES') {
                                let ary=curDataSource.map(item=>{
                                    return item[idField];
                                });
                                this.$refs.rxTree.checkeds=ary;
                            } else if (isDialog == 'YES') {
                                this.selectedRows = curDataSource;
                                this.selectedTable.dataSource = [...curDataSource]
                            }
                        })
                    })
                }
            }
            //数据权限
            if(dialogConfig.pmtAlias){
                parameter.pmtAlias=dialogConfig.pmtAlias;
            }
            let conf = {
                data: parameter,
                curVm: this,
            };
            Dialog.openDialog(conf, function (data) {
                if (data.action == "ok") {
                    let rows = data.data.rows;
                    if (!rows) {
                        rows = data;
                    }
                    if (!(rows instanceof Array)) rows = [rows];
                    //绑定映射行
                    self_.bindRows(dialogConfig, rows);
                    // //绑定当前控件
                    self_.handBindSelf(dialogConfig, rows);
                    if(dialogConfig.callback){
                        self_.formVm[dialogConfig.callback](data);
                    }
                }
            });
        },
        /**
         * 绑定当前控件
         * @param binding
         * @param rows
         */
        handBindSelf(binding, rows) {
            let valueField = binding.useCondSql == 'WEBREQ' || binding.useCondSql == 'INTERFACE' ? binding.valueField : binding.valueField.toUpperCase();
            let aryValue = [];
            for (let i = 0, j = rows.length; i < j; i++) {
                let row = rows[i];
                let value = row[valueField];
                aryValue.push(value);
            }
            this.curValue = aryValue.join(",");
        }
    },
    watch: {
        value: {
            handler(val) {
                this.curValue = val ;
            },
            deep: true,
            immediate: true
        },
        curValue(val){
            this.updateValue(val);
            this.triggerEmitEvents('change',val,this.data)
        },
    },
}
</script>

<style scoped>
.rx-disabled {
    background: rgba(245, 246, 249, 1);
    display: flex;
    align-items: center;
    padding: 0.5rem;
    border-radius: 4px;
    border: none;
    min-height: 1rem;
}
</style>
